#!/usr/bin/env bash

# Copyright (C) 2020  Matthew "strager" Glazar
# See end of file for extended copyright information.

set -e
set -o pipefail
set -u

cd "$(dirname "${0}")/.."
source tools/files.sh

check_file_copyright_header() {
  local file="${1}"
  case "${file}" in
    benchmark/benchmark-lsp/corpus/*) ;;
    benchmark/benchmark-lsp/*) check_file_copyright_header_benchmark_lsp "${file}" ;;
    *) check_file_copyright_header_quick_lint_js "${file}" ;;
  esac
}

# quick-lint-js is GPLv3-licensed.
check_file_copyright_header_quick_lint_js() {
  local file="${1}"
  fgrep -q 'Copyright (C)' "${file}" && \
    fgrep -q 'This file is part of quick-lint-js.' "${file}" && \
    fgrep -q 'quick-lint-js finds bugs in JavaScript programs.' "${file}" && \
    fgrep -q 'quick-lint-js is free software' "${file}"
}

# quick-lint-js's benchmark-lsp subproject is BSD3-licensed.
check_file_copyright_header_benchmark_lsp() {
  local file="${1}"
  fgrep -q 'Copyright' "${file}" && \
    fgrep -q 'This file is part of quick-lint-js.' "${file}"
}

error=0
for file in $(find_non_vendor_files | matching '(\<CMakeLists\.txt|\.bt|\.c|\.cmake|\.cpp|\.go|\.gperf|\.h|\.hs|\.html|\.js|\.mjs|\.nix|\.po|\.pot|\.py|\.sh|\.vim|\.yaml)$'); do
  if ! check_file_copyright_header "${file}"; then
    printf 'error: file missing copyright: %s\n' "${file}"
    error=1
  fi
done
exit "${error}"

# quick-lint-js finds bugs in JavaScript programs.
# Copyright (C) 2020  Matthew "strager" Glazar
#
# This file is part of quick-lint-js.
#
# quick-lint-js is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# quick-lint-js is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with quick-lint-js.  If not, see <https://www.gnu.org/licenses/>.
